﻿using SolidWorks.Interop.sldworks;
//using SolidWorksDll;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SolidWorksDll
{
    public class ModelDocHelp
    {
        //获取零件、装配体、工程图对象
        public static PartDoc GetPart(ModelDoc2 modelDoc)
        {
            if (modelDoc == null)
            {
                return null;

            }
            if (modelDoc.GetType() != 1)
            {
                return null;

            }
            else
            {
                return (PartDoc)modelDoc;
            }

        }

        public static AssemblyDoc GetAsm(ModelDoc2 modelDoc)
        {
            if (modelDoc == null)
            {
                return null;

            }
            if (modelDoc.GetType() != 2)
            {
                return null;

            }
            else
            {
                return (AssemblyDoc)modelDoc;
            }

        }

        public static DrawingDoc GetDrw(ModelDoc2 modelDoc)
        {
            if (modelDoc == null)
            {
                return null;

            }
            if (modelDoc.GetType() != 3)
            {
                return null;

            }
            else
            {
                return (DrawingDoc)modelDoc;
            }

        }

        /// <summary>
        /// 使用指定模板创建新零件
        /// </summary>
        /// <param name="temPath"></param>
        /// <returns></returns>
        public static PartDoc NewPart(string temPath)
        {
            if (SolidWorksHelp.swApp == null)
            {
                MessageBox.Show("请先连接到SolidWorks对象");

                return null;
            }

            ModelDoc2 swModel = SolidWorksHelp.swApp.NewDocument(temPath, 0, 0, 0);
            return GetPart(swModel);
        }

        
        
        /// <summary>
        /// 根据指定模板创建新零件
        /// </summary>
        /// <param name="temPath"></param>
        /// <returns>成功返回空字符串，失败返回原因</returns>
        public static string NewDocumnet(string temPath)
        {
            if (File.Exists(temPath) == false)
            {
                return "模板路径" + temPath + "不存在";
            }
            PartDoc part = NewPart(temPath);

            if (part == null)
            {
                return "创建零件失败";
            }
            else
            {
                return "";
            }
        }

        /// <summary>
        /// 保存模型文件
        /// </summary>
        /// <param name="NewFilePath"></param>
        /// <param name="modelDoc"></param>
        /// <returns></returns>
        public static string SaveDoc(string NewFilePath, object modelDoc = null)
        {
            if (modelDoc == null)
            {
                modelDoc = SolidWorksHelp.swApp.ActiveDoc;
            }
            ModelDoc2 swModel = (ModelDoc2)modelDoc;
            ModelDocExtension modelExt = swModel.Extension;
            return SaveDoc(modelExt, NewFilePath);
        }



        /// <summary>
        /// 获取当前装配体在位编辑的文件对象
        /// </summary>
        /// <returns></returns>
        public static string GetEditTarget()
        {
            ModelDoc2 modelDoc = SolidWorksHelp.swApp.ActiveDoc;
            if (modelDoc == null)
            {
                return "当前文件为空";
            }
            else
            {
                if (modelDoc.GetType() != 2)
                {
                    return "当前文件不是装配体";
                }

                AssemblyDoc asmDoc = (AssemblyDoc)modelDoc;
                ModelDoc2 swModel = asmDoc.GetEditTarget();
                if (swModel == null)
                {
                    return "当前没有在位编辑的文件";
                }
                return swModel.GetPathName();
            }


        }
        /// <summary>
        /// 获取当前SolidWorks打开而且激活的文件对象
        /// </summary>
        /// <returns></returns>
        public static string GetActiveDoc()
        {

            ModelDoc2 modelDoc = SolidWorksHelp.swApp.ActiveDoc;
            if (modelDoc != null)
            {
                return modelDoc.GetPathName();
            }
            return "当前文件为空";
        }

        /// <summary>
        /// 保存文件
        /// </summary>
        /// <param name="modelExt"></param>
        /// <param name="NewFilePath"></param>
        /// <returns>成功返回空字符串，失败返回错误信息</returns>
        public static string SaveDoc(ModelDocExtension modelExt, string NewFilePath)
        {
            int err = 0;
            int war = 0;
            bool b = modelExt.SaveAs(NewFilePath, 0, 1, null, ref err, ref war);
            if (b == false || err != 0)
            {
                return "保存文件到：" + NewFilePath + "出现错误，错误代号：" + err.ToString();

            }
            return "";
        }






        /// <summary>
        /// 打开指定路径的文档
        /// </summary>
        /// <param name="FilePath"></param>
        /// <returns></returns>
        public static string OpenDoc(string FilePath)
        {
            string ext = Path.GetExtension(FilePath).ToUpper();
            string fileName = Path.GetFileName(FilePath);
            int type = 0;
            switch (ext)
            {
                case ".SLDPRT":
                    type = 1;
                    break;
                case ".SLDASM":
                    type = 2;
                    break;
                case ".SLDDRW":
                    type = 3;
                    break;
                default:
                    break;
            }
            if (type == 0)
            {
                return "不是零件、装配体或者工程图";
            }
            int err = 0;
            int war = 0;



            ModelDoc2 swModel = SolidWorksHelp.swApp.ActivateDoc(fileName);
           // ModelDoc2 swModel = SolidWorksHelp.swApp.ActiveDoc;
            if (swModel != null)
            {
                return "当前已打开文件：" + swModel.GetPathName();

            }
            swModel = SolidWorksHelp.swApp.OpenDoc6(FilePath, type, 1, "", ref err, ref war);
            if (swModel == null)
            {
                return "打开文件：" + FilePath + "失败，错误代号：" + err.ToString();
            }
            else if (err != 0)
            {
                return "打开文件：" + FilePath + "出现错误，错误代号：" + err.ToString();

            }
            else
            {
                return "";
            }

        }


        /// <summary>
        /// 打开指定路径的文档
        /// </summary>
        /// <param name="FilePath"></param>
        /// <returns></returns>
        public static string OpenDoc(string FilePath, ref ModelDoc2 swModel)
        {
            string ext = Path.GetExtension(FilePath).ToUpper();
            string fileName = Path.GetFileName(FilePath);
            int type = 0;
            switch (ext)
            {
                case ".SLDPRT":
                    type = 1;
                    break;
                case ".SLDASM":
                    type = 2;
                    break;
                case ".SLDDRW":
                    type = 3;
                    break;
                default:
                    break;
            }
            if (type == 0)
            {
                return "不是零件、装配体或者工程图";
            }
            int err = 0;
            int war = 0;

            //swModel = SolidWorksHelp.swApp.ActivateDoc(fileName);
            swModel = SolidWorksHelp.swApp.ActiveDoc;
            if (swModel != null)
            {
                return "当前已打开文件：" + swModel.GetPathName();

            }
            swModel = SolidWorksHelp.swApp.OpenDoc6(FilePath, type, 1, "", ref err, ref war);
            if (swModel == null)
            {
                return "打开文件：" + FilePath + "失败，错误代号：" + err.ToString();
            }
            else if (err != 0)
            {
                return "打开文件：" + FilePath + "出现错误，错误代号：" + err.ToString();

            }
            else
            {
                return "";
            }

        }






        public static List<string> GetAllDoc()
        {
            if (SolidWorksHelp.swApp == null)
            {
                MessageBox.Show("请先连接到SolidWorks对象");

                return null;
            }
            List<string> modelPaths = new List<string>();

            int count = SolidWorksHelp.swApp.GetDocumentCount();
            if (count == 0)
            {
                return modelPaths;
            }

            var swModels = (object[])SolidWorksHelp.swApp.GetDocuments();
            foreach (ModelDoc2 item in swModels)
            {
                modelPaths.Add(item.GetPathName());
            }
            return modelPaths;

        }



    }
}
